package com.union.vehicleassistant.util.update;

import android.text.TextUtils;

/**
 * 对比版本号
 *
 * @author mz
 * @date-time 2024/12/10 17:43
 */
public class VersionUtil {

    /**
     * 根据versionName对比线上版本versionOnline判断versionName是否需要更新
     *
     * @param versionName   当前版本
     * @param versionOnline 线上对比版本
     */
    public static boolean isNeedUpdate(String versionName, String versionOnline) {
        if (TextUtils.isEmpty(versionName) || TextUtils.isEmpty(versionOnline)) {
            return false;
        }
        if (versionName.equals(versionOnline)) {
            return false;
        }
        String[] currentVersion;
        String[] onlineVersion;

        String[] localeVer = versionName.split("\\.");
        String[] onlineVer = versionOnline.split("\\.");
        if (1 == localeVer.length && 1 == onlineVer.length) {
            currentVersion = versionName.split("_");
            onlineVersion = versionOnline.split("_");
        } else {
            currentVersion = localeVer;
            onlineVersion = onlineVer;
        }
        int minLength = Math.min(currentVersion.length, onlineVersion.length);
        VersionNodeComparator comparator = new VersionNodeComparator();
        for (int i = 0; i < minLength; i++) {
            int compare = comparator.compare(currentVersion[i], onlineVersion[i]);
            if (compare > 0) {
                return false;
            } else if (compare < 0) {
                return true;
            }
        }
        return currentVersion.length < onlineVersion.length;
    }
}
